﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using TarreOA.TarreOAModels;

namespace TarreOA.TarreOADAL {
    public class Flow_Forms_DAO {

        /// <summary>
        /// 根据指定条件查找表单信息
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="sqlParas"></param>
        /// <returns></returns>
        public SqlDataReader FindByTest(string sql, Hashtable sqlParas, SqlConnection sqlConn)
        {
            SqlCommand sqlCmd = null;
            SqlDataReader sqlDr = null;
            try
            {
                sqlConn.Open();
                sqlCmd = new SqlCommand(sql, sqlConn);
                foreach (object key in sqlParas.Keys)
                {
                    sqlCmd.Parameters.Add(new SqlParameter(key.ToString(), sqlParas[key]));
                }
                sqlDr = sqlCmd.ExecuteReader();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
            }
            return sqlDr;
        }
        
        /// <summary>
        /// 获取所有表单信息
        /// </summary>
        /// <returns></returns>
        public List<Flow_Forms> FindAllForms()
        {
            List<Flow_Forms> list = new List<Flow_Forms>();
            SqlConnection conn = null;
            SqlDataReader dr = null;
            try
            {
                conn = SQLManager.GetConn();
                SqlCommand sc = new SqlCommand();
                sc.Connection = conn;
                sc.CommandText = "select * from Flow_Forms";
                conn.Open();
                dr = sc.ExecuteReader();
                while (dr.Read())
                {
                    Flow_Forms f = new Flow_Forms();
                    f.Id = (int)dr["Id"];
                    f.Form_name = (string)dr["Form_Name"];
                    f.Form_model = (string)dr["Form_Model"];
                    list.Add(f);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                if (dr != null)
                {
                    if (!dr.IsClosed)
                    {
                        dr.Close();
                        dr.Dispose();
                    }
                }
                if (conn != null)
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
            return list;
        }
        /// <summary>
        /// 根据表单ID获取表单信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Flow_Forms FindByFormId(int id)
        {
            Flow_Forms f = new Flow_Forms();
            SqlConnection conn = null;
            SqlDataReader dr = null;
            try
            {
                conn = SQLManager.GetConn();
                SqlCommand sc = new SqlCommand();
                sc.Connection = conn;
                sc.CommandText = "select * from Flow_Forms where Id="+id.ToString();
                conn.Open();
                dr = sc.ExecuteReader();
                if (dr.Read())
                {
                    f.Id = (int)dr["Id"];
                    f.Form_name = (string)dr["Form_Name"];
                    f.Form_model = (string)dr["Form_Model"];
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                if (dr != null)
                {
                    if (!dr.IsClosed)
                    {
                        dr.Close();
                        dr.Dispose();
                    }
                }
                if (conn != null)
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
            return f;
        }
    }
}
